package com.bankscene.screenfit.activity;

import android.os.Bundle;
import android.support.annotation.CheckResult;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;

import com.bankscene.screenfit.App;
import com.bankscene.screenfit.core.ActivityStackManager;
import com.bankscene.screenfit.handler.BackHandlerHelper;
import com.bankscene.screenfit.util.ActivityUtils;
import com.bankscene.screenfit.vus.Vu;

import org.greenrobot.eventbus.EventBus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public abstract class BasePresenterActivity<V extends Vu> extends AppCompatActivity {

    private Logger log = LoggerFactory.getLogger(BasePresenterActivity.class);
    protected V vu;
    protected FragmentManager fm;
    protected EventBus bus;
    protected App app;
    private String simpleName;
    protected ActivityStackManager activityStackManager;

    @Override
    public final void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activityStackManager = ActivityStackManager.newInstance();
        activityStackManager.push(this);

        simpleName = getClass().getSimpleName();
        log.info("{} onCreate called... ", simpleName);
        app = App.getInstance();
        fm = getSupportFragmentManager();
        if (isUseEventBus()) {
            bus = EventBus.getDefault();
        }
        try {
            vu = getVuClass().newInstance();
            vu.init(getLayoutInflater(), null, bus);
            setContentView(vu.getRootView());

            ActivityUtils.setupWindowAnimations(this);

            onBindVu();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    @Override
    public final void onRestart() {
        super.onStart();
        log.info("{} onRestart called...", simpleName);
    }

    @Override
    public final void onStart() {
        super.onStart();
        log.info("{} onStart called...", simpleName);
    }

    @Override
    public final void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        log.info("{} onRestoreInstanceState called...", simpleName);
    }

    @Override
    public final void onResume() {
        log.info("{} onResume called...", simpleName);
        super.onResume();
        afterResume();
    }

    @Override
    public final void onPause() {
        log.info("{} onPause called...", simpleName);
        beforePause();
        super.onPause();
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        log.info("{} onSaveInstanceState called... {}", simpleName, outState);
    }

//    java.lang.NoClassDefFoundError: android/os/PersistableBundle 4+
//    @Override
//    public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
//        log.info("{} onSaveInstanceState called... {}", simpleName, outState);
//        super.onSaveInstanceState(outState, outPersistentState);
//    }

    @Override
    protected final void onStop() {
        log.info("{} onStop called...", simpleName);
        super.onStop();
    }

    @Override
    protected final void onDestroy() {
        log.info("{} onDestroy called...", simpleName);
        onDestroyVu();
        vu.onUnwrap();
        vu = null;
        super.onDestroy();
    }

    protected void afterResume() {
        if (isUseEventBus()) {
            bus.register(this);
        }
    }

    protected void beforePause() {
        if (isUseEventBus()) {
            bus.unregister(this);
        }
    }

    @Override
    public final void onBackPressed() {
        log.info("{} onBackPressed called...", simpleName);
        if (!handleBackPressed()) {
            if (isUseEventBus()) {
                bus.removeAllStickyEvents();
            }
            super.onBackPressed();
        }
    }

    @CheckResult
    public boolean handleBackPressed() {
        log.info("{} handleBackPressed called...", simpleName);
        return BackHandlerHelper.handleBackPress(this);
    }

    @CheckResult
    protected abstract Class<V> getVuClass();

    protected void onBindVu() {}

    protected void onDestroyVu() {}

    @CheckResult
    protected boolean isUseEventBus() {
        return true;//默认使用EventBus,如子类不需要使用,请复写该方法
    }

}
